package com.zengjianjun.oss.platform;

import com.zengjianjun.oss.enums.OssPlatformEnum;
import com.zengjianjun.oss.exception.OssException;
import com.zengjianjun.oss.platform.aliyun.AliYunOssClient;
import com.zengjianjun.oss.platform.amazon.AmazonOssClient;
import com.zengjianjun.oss.platform.param.FileListParam;
import com.zengjianjun.oss.platform.result.FileListResult;
import com.zengjianjun.oss.platform.tencent.TencentCosClient;
import com.zengjianjun.oss.properties.OssProperties;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @author zengjianjun
 */
@Slf4j
public class OssClient implements IOssTypeClient, IOssClient {

    @Resource
    private OssProperties ossProperties;
    private IOssClient defaultClient;
    private IOssClient aliyunOssClient;
    private IOssClient amazonOssClient;
    private IOssClient tencentCosClient;


    @PostConstruct
    public void init() {
        // 通过new OssClient(IOssClient client)方式创建的不再初始化其它oss连接对象
        if (defaultClient == null) {
            log.info("oss client init start...");
            OssPlatformEnum type = ossProperties.getType();
            if (type == null) {
                throw OssException.build("未设置默认oss类型");
            }
            if (ossProperties.getAliyun() != null) {
                aliyunOssClient = new AliYunOssClient(ossProperties.getAliyun());
            }
            if (ossProperties.getAmazon() != null) {
                amazonOssClient = new AmazonOssClient(ossProperties.getAmazon());
            }
            if (ossProperties.getTencent() != null) {
                tencentCosClient = new TencentCosClient(ossProperties.getTencent());
            }
            // 设置默认ossClient
            switch (type) {
                case aliyun:
                    defaultClient = aliyunOssClient;
                    break;
                case amazon:
                    defaultClient = amazonOssClient;
                    break;
                case tencent:
                    defaultClient = tencentCosClient;
                default:
            }
            log.info("oss default client is {}", type.name());
            log.info("oss client init success...");
        }
    }

    public OssClient() {

    }

    public OssClient(IOssClient client) {
        this.defaultClient = client;
    }

    private IOssClient getOssClient(OssPlatformEnum type) {
        if (type == null) {
            return defaultClient;
        }
        switch (type) {
            case aliyun:
                return aliyunOssClient;
            case amazon:
                return amazonOssClient;
            case tencent:
                return tencentCosClient;
        }
        throw OssException.build("oss类型错误");
    }

    @Override
    public List<FileListResult> getFileList(FileListParam param) {
        return defaultClient.getFileList(param);
    }

    @Override
    public FileListResult getFile(String bucketName, String fileKey) {
        return defaultClient.getFile(bucketName, fileKey);
    }

    @Override
    public List<FileListResult> getFullFileList(FileListParam param) {
        return defaultClient.getFullFileList(param);
    }

    @Override
    public void download(String bucketName, String fileKey, String filePath) {
        defaultClient.download(bucketName, fileKey, filePath);
    }

    @Override
    public void upload(String bucketName, String fileKey, String filePath) {
        defaultClient.upload(bucketName, fileKey, filePath);
    }

    @Override
    public void upload(String bucketName, String fileKey, File file) {
        defaultClient.upload(bucketName, fileKey, file);
    }

    @Override
    public void delete(String bucketName, String fileKey) {
        defaultClient.delete(bucketName, fileKey);
    }

    @Override
    public List<FileListResult> getFileList(OssPlatformEnum type, FileListParam param) {
        return this.getOssClient(type).getFileList(param);
    }

    @Override
    public FileListResult getFile(OssPlatformEnum type, String bucketName, String fileKey) {
        return this.getOssClient(type).getFile(bucketName, fileKey);
    }

    @Override
    public List<FileListResult> getFullFileList(OssPlatformEnum type, FileListParam param) {
        return this.getOssClient(type).getFullFileList(param);
    }

    @Override
    public void download(OssPlatformEnum type, String bucketName, String fileKey, String filePath) {
        this.getOssClient(type).download(bucketName, fileKey, filePath);
    }

    @Override
    public void upload(OssPlatformEnum type, String bucketName, String fileKey, String filePath) {
        this.getOssClient(type).upload(bucketName, fileKey, filePath);
    }

    @Override
    public void upload(OssPlatformEnum type, String bucketName, String fileKey, File file) {
        this.getOssClient(type).upload(bucketName, fileKey, file);
    }

    @Override
    public void delete(OssPlatformEnum type, String bucketName, String fileKey) {
        this.getOssClient(type).delete(bucketName, fileKey);
    }
}
